package com.yjhealth.libs.businessdemo.business;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.View;

import com.yjhealth.libs.businessdemo.R;
import com.yjhealth.libs.businessdemo.adapter.DemoViewDelegate;
import com.yjhealth.libs.businessdemo.databinding.BusinessdemoActivityDemoListBinding;
import com.yjhealth.libs.commonlib.base.activity.BaseListActivity;
import com.yjhealth.libs.core.core.activity.CoreListActivity;
import com.yjhealth.libs.core.log.LogUtil;
import com.yjhealth.libs.core.view.recyclerview.RecyclerViewUtil;
import com.yjhealth.libs.core.view.recyclerview.adapter.CoreListAdapter;
import com.yjhealth.libs.core.view.recyclerview.adapter.ItemViewDelegate;

import java.util.ArrayList;

public class DemoListActivity extends BaseListActivity {
    /*Default*/
    /*Util*/
    private Handler handler = new Handler();
    private CoreListAdapter<String> adapter;
    /*Flag*/
    private ArrayList<String> datas;
    private int flag;
    /*View*/
    private BusinessdemoActivityDemoListBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.businessdemo_activity_demo_list);
        initLayout();
        initRecyclerView();
        getData1();
        showLoadingView();
        handler.postDelayed(runnable1, 3000);
    }

    @Override
    protected void onRefreshView() {
        showLoadingView();
        LogUtil.d("DemoListActivity;onRefreshView;" + pageNo);
        if (flag == 0) {
            flag = 1;
            handler.postDelayed(runnable2, 1000);
        } else {
            handler.postDelayed(runnable6, 1000);
        }

    }

    @Override
    protected void onLoadMoreView() {
        LogUtil.d("DemoListActivity;onLoadMoreView;" + pageNo);
        showLoadingView();
        if (pageNo == 2) {
            handler.postDelayed(runnable5, 1000);
        } else if (pageNo == 4) {
            handler.postDelayed(runnable4, 1000);
        } else {
            handler.postDelayed(runnable3, 1000);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable1);
        handler.removeCallbacks(runnable2);
        handler.removeCallbacks(runnable3);
        handler.removeCallbacks(runnable4);
        handler.removeCallbacks(runnable5);
    }

    private void initRecyclerView() {
        RecyclerViewUtil.initLinearV(activity, binding.recyclerView, R.color.yjhealth_core_divider, R.dimen.dp_0_5, R.dimen.dp_15, R.dimen.dp_0);
        DemoViewDelegate viewDelegate = new DemoViewDelegate();
        viewDelegate.setOnItemClickListener(new ItemViewDelegate.OnItemClickListener<String>() {
            @Override
            public void onItemClick(@NonNull ArrayList<String> datas, int position) {
                LogUtil.d("DemoListActivity;onItemClick;position=" + position);
            }

            @Override
            public boolean onItemLongClick(@NonNull ArrayList<String> datas, int position) {
                LogUtil.d("DemoListActivity;onItemLongClick;position=" + position);
                return true;
            }

            @Override
            public void onItemViewClick(@NonNull View view, @NonNull ArrayList<String> datas, int position) {
                if (view.getId() == R.id.tvText) {
                    LogUtil.d("DemoListActivity;onItemViewClick;position=" + position);
                }
            }

            @Override
            public boolean onItemViewLongClick(@NonNull View view, @NonNull ArrayList<String> datas, int position) {
                return false;
            }
        });
        adapter = new CoreListAdapter<>(activity, viewDelegate);
        binding.recyclerView.setAdapter(adapter);
    }

    private Runnable runnable1 = new Runnable() {
        @Override
        public void run() {
            showErrorView(null, null);
        }
    };

    private Runnable runnable2 = new Runnable() {
        @Override
        public void run() {
            showEmptyView();
        }
    };

    private Runnable runnable6 = new Runnable() {
        @Override
        public void run() {
            restoreView();
            getData();
        }
    };

    private Runnable runnable3 = new Runnable() {
        @Override
        public void run() {
            restoreView();
            addData();
        }
    };

    private Runnable runnable4 = new Runnable() {
        @Override
        public void run() {
            showEmptyView();
        }
    };

    private Runnable runnable5 = new Runnable() {
        @Override
        public void run() {
            showErrorView(null, null);
            pageNo = 2;
        }
    };

    private void getData() {
        datas = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            datas.add("test=" + i);
        }

        adapter.setData(datas);
    }

    private void getData1() {
        datas = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            datas.add("test=" + i);
        }

        adapter.setData(datas);
    }

    private void addData() {
        ArrayList<String> newDatas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            newDatas.add("add=" + i);
        }

        adapter.addData(newDatas);
    }
}
